home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Games of Daze
/
Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso
/
x2ftp
/
msdos
/
vla
/
ctut3vla
/
array.txt
< prev
next >
Wrap
Text File
|
1993-06-11
|
7KB
|
136 lines
[%] An Explanation of Arrays [%]
By : Desolation/VLA
Here is the next tutorial in C which tells you about arrays. Arrays are
a very important topic in programming and you will use it a lot, so I thought
I would start it out with an explanation of its usage, how to implement it,
etc. The last tutorial on loops has yet to be fully completed, but you
should learn how to do the other looping structures on your own such as the
while loop, the do-while loop, etc. It's not that hard to figure out, they
are pretty much the same.
Ok, here we go. An array is just what it sounds like. It's an assortment
of the same type of objects in an ordered fashion which has just one variable
name associated with it. It has an index which allows you to access each part
of the array easily. In the actual computer, this would be a separate memory
location for each individual part of the array (just trivia). A simple way to
visualize an array would be like this:
Let's say you make an array of integers called "integer_array" and want the
size of the array to be of length 5. This is what it might look like :
┌───────────────────┐
integer_array │ 2 │321│ 12│576│378│ <- Integer Values
(array name) └───────────────────┘
0 1 2 3 4 <- Index Numbers
First off, the numbers in each slot isn't specific, just in case you're
confused. They are whatever you want to put in there, just as long as they
are all of the type you specified in the beginning (which was of type integer).
NOTICE! This is very important to remember. The index begins with 0, not
1. So when you go to reference the first element in the array, you use
0 and not 1. This is important because some other languages use 1 to begin
with. This also makes the ending index number 1 off. In our example I said
of length 5, which means 5 elements, but the numbers go from 0 to 4. If you
look, there are 5 elements there, so keep this in mind, because if you
are going through writing code that uses arrays, and miss this fact, you'll
screw up your code.
The index of the array is used to pull out specific elements in the array.
For example if you look at the index #3 of the integer_array we declared, the
value there would be "576". Nothing too difficult, but how does this all fit
into C you might ask? Well, as I run you around some more, I'll final give
you the low down on how this is actually implemented in C.
The formal declaration looks like this : type variable_name[size];
Type is any type you want it to be, integer, float, character, etc.
Variable_Name is the name you give it (for example integer_array like before).
And the size is the number of elements you want in the array (5 in our
example from before.)
For our example, here is what it would like look if we declared it in C :
int integer_array[5];
Got that? Pretty simple so far eh? Now, let's say we haven't assigned
anything to our array yet as we did in our previous example. How do we
get those values into the array? Well, it should do something with the
index value right? How else would we access the elements without the index?
After we declare the array, we can assign the values in this way :
integer_array[0] = 2;
integer_array[1] = 321;
integer_array[2] = 12;
integer_array[3] = 576;
integer_array[4] = 378;
The value in the brackets now acts as the index value. The first one says
to assign the integer value "2" to the first element in the array (element 0).
The second assignment statement says to assign "321" to the 2nd element in
the array (element 1) and so on. No problems.
Now for a little application. To assign to the array, you will hardly
every assign every index one by one like I did up above. Instead, you use
a variable for the indexing value and store that way. Let me show you some
code first :
int i;
int integer_array[5];
for(i=0; i<5; i++) integer_array[i] = 400;
Now looking at this code, what does it do? First off we declared our array
and another integer which I called 'i'. This will be used as the indexing
value. We start up by using a for loop structure. This will allow us to
increment 'i' by 1 every time we go through starting from 0 and going to 5.
So we start at i=0. Then we execute the next part which says
"integer_array[i] = 400;"
But 'i' is currently 0 right now correct? So, integer_array[0] gets the
value of 400. Then we go back to the for looping structure because we
aren't done yet. We increment 'i' by 1 to 1 (because we started at 0) and
execute the rest again. Now since 'i' is 1, we say integer_array[1] = 400
which would stick the value of 400 in the #1 slot of the array and so on.
This is what it would look like when we finish :
┌───────────────────┐
integer_array │400│400│400│400│400│ <- Integer Values
(array name) └───────────────────┘
0 1 2 3 4 <- Index Numbers
We stared at index 0, put in the value of 400, then incremented the index
value by 1, stored in the value of 400, and kept going until we got to the
end of the array. A for loop is commonly used for arrays because you can
access the index values like a variable and store easily into each element
of the array.
Now it's time for you to play with this and see what you can do with it.
After all, you are the one who needs to figure out how to use this in a more
complex way to suit your own needs. A good place to start is to use a
variable for an assignment, such as :
integer_array[i] = some_variable_with_some_operation;
and so on. Try creating a small program that will prompt for 5 integer
values, then store each value into the array using a for loop. Then pull
out and examine the values in the array and print them back to the screen.
Once again, if you need ANY help or have ANY questions, ask on Phantasm.
I would be more than willing to help you out. Arrays are very important, and
I wouldn't be surprised if you use them in a great deal of your coding once
you start writing anything moderatly complex, so ask away and understand
what this is all about! In any event, may your code be bug free and
efficient!
Desolation/VLA
decko@u.washington.edu
Phantasm (206)232-5912
Now supporting USR Dual Standard
Coming Up Next Time : Multi-dimensional arrays? Pointers and array pointers?
Who knows? I'm not organized enough to know.